package com.jl.basicframework.ui.customizeView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.jl.basicframework.R;
import com.jl.basicframework.utils.dataProcessingUtils.FormatUtils;

/***
 * @author 作者:姜澜
 * 时间:2022/3/11
 * 邮箱:316879104@pp.com
 * 类简述: 自定义头部标题组合控件
 */
public class HeadTileView extends LinearLayout {

    /**
     * 返回按键
     */
    private ImageView offImgView;

    /**
     * 标题栏
     */
    private TextView tileView;

    /**
     * 副标题栏
     */
    private TextView subTileView;

    public HeadTileView(Context context) {
        super(context);
        bindView(context);
    }

    public HeadTileView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bindView(context);
        initAttrs(context, attrs);
    }

    public HeadTileView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bindView(context);
        initAttrs(context, attrs);
    }

    private void bindView(Context context){
        LayoutInflater.from(context).inflate(R.layout.view_head_tile,this,true);
        offImgView = findViewById(R.id.off);
        tileView = findViewById(R.id.tile);
        subTileView = findViewById(R.id.subTitle);

        setOffKey();
    }

    /**
     * 初始化attrs
     */
    public void initAttrs(Context context,AttributeSet attrs){
        @SuppressLint("Recycle")
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.HeadTileView);

        String tile = mTypedArray.getString(R.styleable.HeadTileView_headTileText);
        if (FormatUtils.isStringNONull(tile)){
            tileView.setText(tile);
        }

        String subTile = mTypedArray.getString(R.styleable.HeadTileView_subTileText);
        if (FormatUtils.isStringNONull(subTile)){
            subTileView.setText(subTile);
        }

        //设置标题颜色
        tileView.setTextColor(
                mTypedArray.getColor(R.styleable.HeadTileView_headTileColor,
                        context.getResources().getColor(R.color.title_text_color)));

        //设置副标题颜色
        subTileView.setTextColor(
                mTypedArray.getColor(R.styleable.HeadTileView_subTileColor,
                        context.getResources().getColor(R.color.title_text_color)));

        int offImageVisibility = mTypedArray.getInt(R.styleable.HeadTileView_offImageVisibility,0);
        switch (offImageVisibility){
            case 0:
                offImgView.setVisibility(VISIBLE);
                break;
            case 1:
                offImgView.setVisibility(INVISIBLE);
                break;
            case 2:
                offImgView.setVisibility(GONE);
                break;
            default:

                break;
        }
        setOffKey();
    }

    /**
     * 设置返回健
     */
    private void setOffKey(){
        offImgView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });
    }

    /**
     * 手动设置返回逻辑
     * @param listener
     * @return
     */
    public HeadTileView setOffKey(OnClickListener listener){
        offImgView.setOnClickListener(listener);
        return this;
    }

    /**
     * 设置标题栏
     * @param tileString
     */
    public void setTile(String tileString) {
        this.tileView.setText(tileString);
    }

    /**
     * 设置副标题
     * @param subTileString
     */
    public void setSubTile(String subTileString) {
        this.subTileView.setText(subTileString);
    }

    /**
     * 设置副标题点击事件
     */
    public void setSubTileClick(OnClickListener listener){
        subTileView.setOnClickListener(listener);
    }
}
